﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WithDelegates
{
    public class MainMenu
    {
        private const int k_ExitValue = 0;
        private readonly List<MenuItem> r_ListOfMenuItems = new List<MenuItem>();
        private bool m_IsValidUserChosenOption = false;
        private int m_UserChosenOption = 0;
        private int m_MainMenuLevel = 1;
        private int m_MinMenuValue = 0;
        private int m_MaxMenuValue = 0;

        public void AddMenuItem(MenuItem i_MenuItem)
        {
            if (i_MenuItem != null)
            {
                r_ListOfMenuItems.Add(i_MenuItem);
                m_MaxMenuValue++;
            }
        }

        public void Show()
        {
            while (!m_IsValidUserChosenOption)
            {
                try
                {
                    printMainMenuItems();
                    handleUserChosenOption();
                    
                    if (m_UserChosenOption.Equals((int)MenuItemException.eMenuItemMessage.Exit))
                    {
                        m_IsValidUserChosenOption = true;
                    }
                    else
                    {
                        r_ListOfMenuItems[m_UserChosenOption - 1].OnClick();
                    }
                }
                catch (MenuItemException e)
                {
                    if (e.MenuItemMessage.Equals(MenuItemException.eMenuItemMessage.OutOfRangeInput))
                    {
                        printInvalidInputError();
                    }
                }
                catch (Exception e)
                {
                    printInvalidInputError();
                }
            }
        }

        private void printMainMenuItems()
        {
            System.Console.Clear();
            System.Console.WriteLine("----- Level {0} - {1} -----" + System.Environment.NewLine, m_MainMenuLevel, this.ToString());
            System.Console.WriteLine("Please choose option {0} to {1} or Exit:" + System.Environment.NewLine, m_MinMenuValue + 1, m_MaxMenuValue);
            int index = 0;

            System.Console.Write("{0}. Exit" + System.Environment.NewLine, index);

            foreach (MenuItem item in r_ListOfMenuItems)
            {
                index++;
                System.Console.Write("{0}. {1}" + System.Environment.NewLine, index, item.ToString());
            }
        }

        private void printInvalidInputError()
        {
            System.Console.Clear();
            System.Console.WriteLine("Invalid chosen option, please choose option from {0} to {1} or Exit", m_MinMenuValue + 1, m_MaxMenuValue);
            System.Threading.Thread.Sleep(2500);
        }

        private void handleUserChosenOption()
        {
            string CurrentUserChosenOption = System.Console.ReadLine();

            m_UserChosenOption = int.Parse(CurrentUserChosenOption);

            if (!isValueInOptionsRange(m_UserChosenOption))
            {
                throw new MenuItemException(MenuItemException.eMenuItemMessage.OutOfRangeInput);
            }
        }

        private bool isValueInOptionsRange(int i_Value)
        {
            bool result = false;

            if (i_Value >= m_MinMenuValue && i_Value <= m_MaxMenuValue)
            {
                result = true;
            }

            return result;
        }

        public override string ToString()
        {
            return "Main Menu";
        }
    }
}